package com.queming.music.controller;

import com.queming.music.dto.PlayListCreateRequest;
import com.queming.music.dto.PlayListSearchFilter;
import com.queming.music.dto.RecommendRequest;
import com.queming.music.mapper.PlayListMapper;
import com.queming.service.playlist.PlayListService;
import com.queming.music.vo.PlayListVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

/**
 * @author chencidiao
 * @date 2022/05/05 21:58
 **/
@RestController
@RequestMapping("/playlist")
public class PlayListController {

    private PlayListService service;
    @Autowired
    public void setService(PlayListService service) {
        this.service = service;
    }

    private PlayListMapper mapper;
    @Autowired
    public void setMapper(PlayListMapper mapper) {
        this.mapper = mapper;
    }

    @GetMapping("/{id}")
    public PlayListVo get(@PathVariable String id){
        return mapper.toVo(service.get(id));
    }


    @PostMapping
    @PreAuthorize("hasRole('ROLE_ADMIN')")
    public PlayListVo create(@Validated @RequestBody PlayListCreateRequest playListCreateRequest){
        return mapper.toVo(service.create(mapper.toDto(playListCreateRequest)));
    }

    @GetMapping
    public Page<PlayListVo> search(@Validated PlayListSearchFilter playListSearchFilter){
        return service.search(playListSearchFilter).map(mapper::toVo);
    }

    @PostMapping("/{id}/recommend")
    @PreAuthorize("hasRole('ROLE_ADMIN')")
    public PlayListVo recommend(@PathVariable String id, @Validated @RequestBody RecommendRequest recommendRequest){
        return mapper.toVo(service.recommend(id, recommendRequest.getRecommendedFactor()));
    }

    @PostMapping("/{id}/cancel_recommendation")
    @PreAuthorize("hasRole('ROLE_ADMIN')")
    public PlayListVo cancelRecommendation(@PathVariable String id){
        return mapper.toVo(service.cancelRecommendation(id));
    }


    @PostMapping("/{id}/make_special")
    @PreAuthorize("hasRole('ROLE_ADMIN')")
    public  PlayListVo makeSpecial(@PathVariable String id){
        return mapper.toVo(service.makeSpecial(id));
    }

    @PostMapping("/{id}/cancel_special")
    @PreAuthorize("hasRole('ROLE_ADMIN')")
    public  PlayListVo cancelSpecial(@PathVariable String id){
        return mapper.toVo(service.cancelSpecial(id));
    }























}
